package com.tedu.egweibo2.exception;

import com.tedu.egweibo2.result.JsonResult;
import com.tedu.egweibo2.result.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler
    public JsonResult doHandlerRuntimeException(RuntimeException r){
        log.error("用户名不能为空");
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }

    @ExceptionHandler
    public JsonResult doHandlerMethodArgumentNotValidException(MethodArgumentNotValidException e){
        String message = e.getFieldError().getDefaultMessage();
        log.error(message,e);
        return new JsonResult(StatusCode.VALIDATE_ERROR,message);
    }
}
